function _setState(newState) {
  this.state = { ...newState };
  this.subscriptions.forEach((subscription) => {
    subscription(this.state);
  });
}

function _useCustom(React) {
  // eslint-disable-next-line no-unused-vars
  const [_, subscription] = React.useState();
  React.useEffect(() => {
    this.subscriptions.push(subscription);
    return () => {
      this.subscriptions = this.subscriptions.filter((item) => item !== subscription);
    };
  }, []);
  return [this.state, this.setState];
}

export default function useSharedState(React, initState) {
  const store = {
    state: initState,
    subscriptions: [],
  };
  store.setState = _setState.bind(store);
  return _useCustom.bind(store, React);
}
